
const Input = {};
const State = {
    stand: 1,
    attack: 2,
    hurt: 3,
    jump:4,
    attackAir: 5,
    attackSlide: 6,
};
cc.Class({
    extends: cc.Component,

    properties: {
        daggerPrefab:{
            default:null,
            type:cc.Prefab,
        },
        roleFramePrefab:{
            default:null,
            type:cc.Prefab
        },
        roleAnim:{
            default:null,
            type:cc.Animation
        },
        maxHp:5,
        maxMp:5,
        MprestoreTime:4
    },

    //血量数值更新
    showHp:function (string) {
        this.showHpNode = this.hpNode.getChildByName('HpShow');
        this.HpShowLabel = this.showHpNode.getComponent(cc.Label);
        this.HpShowLabel.string = string;
    },
    //蓝量数值更新
    showMp:function(string){
        this.showMpNode = this.mpNode.getChildByName('MpShow');
        this.MpShowLabel = this.showMpNode.getComponent(cc.Label);
        this.MpShowLabel.string = string;
    },

    onLoad () {
        
        this.roleState = State.stand; //角色状态
        this.rlv = this.node.getComponent(cc.RigidBody);
        this.roleFrameNode = cc.instantiate(this.roleFramePrefab);
        this.node.parent.addChild(this.roleFrameNode);
        this.hpNode = cc.find('Canvas/roleFrame/HP');
        this.roleHp = this.hpNode.getChildByName('roleHp');
        this.hpbar = this.roleHp.getComponent(cc.ProgressBar);

        this.mpNode = cc.find('Canvas/roleFrame/MP');
        this.roleMp = this.mpNode.getChildByName('roleMp');
        this.mpbar = this.roleMp.getComponent(cc.ProgressBar);

        this.roleMusic = this.node.getComponent(cc.AudioSource);
         
        this.rlv.fixedRotation = true; //固定角色初始旋转值

        this.roleAnim.on('finished', this.onAnimFinished, this);

        cc.systemEvent.on('keydown', this.onKeyDown, this);
        cc.systemEvent.on('keyup', this.onKeyUp, this);
    },

    onDestroy(){
        //this.roleAnim.off('finished', this.onAnimFinished, this);
        cc.systemEvent.off('keydown', this.onKeyDown, this);
        cc.systemEvent.off('keyup', this.onKeyUp, this);
    },

    start () {
        this.hp = this.maxHp; //血条
        this.mp = this.maxMp; //蓝条
        this.anim = 'idle';
        this.music = '';
        this.speedX = 200; //移动速度
        this.sp = cc.v2(0, 0);
        this.combo = 0; //attack连击状态
        this.isHit = false; //受击开关

        this.jumpCD = 0; //跳跃冷却时间 
        this.attAirCD = 0; 
        this.attactCD = 0;
        this.daggerCD = 0; //暗器冷却时间
        
        this.now_Time = 0;
        this.jumpHight = 120 // 跳跃高度
        this.run = true;

        this.AnKeyboard = 0; //按键记录
        this.insX = 300; //瞬移距离

        this.showHp(this.hp + "/" + this.maxHp);
        this.showMp(this.mp + "/" + this.maxMp);
    },

    playMusic:function(music){
        this.roleMusic.play(music);
    },
    pauseMusic:function(music){
        this.roleMusic.pause(music);
    },

   
    setAnim:function(anim){
        if (this.anim == anim) {
            return;
        }
        this.anim = anim;
        this.roleAnim.play(anim);
    },

    onAnimFinished:function(e, data){
        if (data.name == 'attack1' || data.name == 'attack2' || data.name == 'attack3' ) {
            this.roleState = State.stand;
            this.playMusic('attack');
          // this.setAnim('idle');
           this.combo = (this.combo + 1) % 3; 
           setTimeout(() => {
            if (this.roleState == State.attack) {
                return;
            }
            this.combo = 0;
           }, 300);
        }else if (data.name == 'attackAir3') {
            this.roleState = State.stand;
            this.setAnim('idle'); 
        }else if (data.name == 'cast') {
            this.roleState = State.stand;
            this.setAnim('idle');
        }else if (data.name == 'hurt') {
            //console.log(data.name);
            this.isHit = false;
            this.attAirCD = 0; 
            this.attactCD = 0;
            if (this.hp > 0) {
                this.hp = --this.hp;
                this.hpbar.progress = this.hp / this.maxHp;
                this.showHp(this.hp+"/"+this.maxHp);
               // console.log(this.hp);
               this.setAnim('idle');
            } 
            if (this.hp == 0) {
                this.setAnim('die');
                createAlert(this.node.parent,'游戏失败，是否重新开始', function(){
                    cc.log('成功');
                });
                return;
            }
            this.roleState = State.stand;
        }else if (data.name == 'jump' || data.name =='attackAir2') {
            this.setAnim('idle');
        }
    },

    onKeyDown(e){
        Input[e.keyCode] = 1;
       // console.log(Input);
    },

    onKeyUp(e){
        Input[e.keyCode] = 0;
    },
  
    //攻击状态
    setAttack:function(){
         this.lv = this.rlv.linearVelocity;
        if (Input[cc.macro.KEY.j]) {
            //anim = 'attack1';
            if (this.combo == 0) {
                this.setAnim('attack1');
            }else if (this.combo == 1) {
                //anim = 'attack2';
                this.setAnim('attack2');
            }else if (this.combo == 2) {
                //anim = 'attack3';
                this.setAnim('attack3');
            }
            this.lv.x =0;
        }else if(Input[cc.macro.KEY.k]) {
            this.combo = 0;
            if (this.attactCD >0) {
                //console.log(this.attactCD); 
                return;
            }
            
            this.attactCD = 15;
            this.roleState = State.stand;
            this.setAnim('attackAir3');
            this.lv.x = 0;
        }else if (Input[cc.macro.KEY.u]) {
            this.lv.x = 0;
            this.setAnim('cast');
            this.setDagger();
            // this.roleState = State.stand;
        }
        this.rlv.linearVelocity = this.lv;
    },

    setDagger:function(){
        if (this.daggerCD > 0 || this.mp == 0) {
            return;
        }
        var pos_parent = this.node.getPosition();
        if(this.mp > 0){
            var parentNode = this.node.parent;
            var dagger = cc.instantiate(this.daggerPrefab);
            parentNode.addChild(dagger);
            pos_parent.y += 55; 
            if (this.node.scaleX == 1) {
              //  dagNode.scaleX = this.node.scaleX;
                pos_parent.x += 40;
            }else if (this.node.scaleX == -1) {
                //dagNode.scaleX = -1;
                pos_parent.x -= 40;
            }
            this.daggerCD = 10;
            this.mp --;
            this.mpbar.progress = this.mp/this.maxMp;
            this.showMp(this.mp+"/"+this.maxMp);
        }
       
        dagger.setPosition(pos_parent);
    },


    //移动状态
    
    move:function(){
        this.lv = this.rlv.linearVelocity;
        // const scaleX = Math.abs(this.node.scaleX);
        const scaleX = Math.abs(this.node.scaleX);
     //  console.log(pos_x);
        if (Input[cc.macro.KEY.a]) {
            this.sp.x = -1;
            this.node.scaleX = -scaleX;
            //anim = 'run';
            this.setAnim('run');
           
        }else if (Input[cc.macro.KEY.d ]) {
            this.sp.x = 1;
            this.node.scaleX = scaleX;
            //anim = 'run';
            this.setAnim('run');
        }else{
            this.sp.x =0;
            //anim = 'idle';
            this.setAnim('idle');
        }


        if (this.sp.x) {
            this.lv.x = this.speedX * this.sp.x;
        }else{
            this.lv.x = 0;
        }

        this.rlv.linearVelocity = this.lv;

        //实现瞬移
        if (Input[cc.macro.KEY.l]) {
            this.setInstant();
        }
        this.isHit = false;
       
    },

    setInstant:function(){
        var worldBorderLeft = -597;
        var wordlBorderRight = 597;
        this.pos = this.node.getPosition();
        var pos_x = this.pos.x;
        var subLeftX =  worldBorderLeft - pos_x;
        var subRightX = wordlBorderRight - pos_x;
        var tw  = cc.tween(this.node).call(()=>{
            this.isHit = true;
        });
        
        if (this.node.scaleX == 1) {
            if (subRightX < this.insX) {
                this.instantRight = cc.tween(this.node).by(0.05, {position: cc.v2(subRightX, 0) }, {easing: 'sineOut'});
                tw.start();
                this.instantRight.start();
            }else{
                this.instantRight = cc.tween(this.node).by(0.1, {position: cc.v2(this.insX, 0) }, {easing: 'sineOut'});
                tw.start();
                this.instantRight.start();
            }
        }else if(this.node.scaleX == -1){
            if (Math.abs(subLeftX) < this.insX) {
                this.instantLeft = cc.tween(this.node).by(0.05, {position: cc.v2(subLeftX, 0) }, {easing: 'sineOut'})
                tw.start();
                this.instantLeft.start();
            }else{
                this.instantLeft = cc.tween(this.node).by(0.1, {position: cc.v2(-this.insX, 0) }, {easing: 'sineOut'})
                tw.start();
                this.instantLeft.start();
            }
        }
       
    },

    setJumpAttack:function(){

        if (this.attAirCD > 0 ) {
            return;
       }
       
        if (this.jumpCD >= 30 && this.jumpCD <= 80) {
           
            if (Input[cc.macro.KEY.k]) {
                this.lv.x = 0;
                this.lv.y = -220;
                this.setAnim('attackAir1');
                this.attAirCD = 50;
               
            }
            this.rlv.linearVelocity = this.lv;
        }
        if (this.jumpCD >=20 && this.jumpCD <=80) {
            if(Input[cc.macro.KEY.j]) {
                this.lv.x = 0;
                this.setAnim('attackAir2');
                
            }
        }
       
    },

    setJump:function(){
        this.lv = this.rlv.linearVelocity;
        const scaleX = Math.abs(this.node.scaleX);
       
        if (this.jumpCD >0) {
            this.setJumpAttack();
          //  console.log(this.jumpCD);
            return;
        }
       
        if (Input[cc.macro.KEY.w]) {
            this.jumpCD = 100;
            this.lv.y = 300;
            this.setAnim('jump');
        }
       
        this.rlv.linearVelocity = this.lv;
        this.roleState = State.stand;
       
    },

    isHurt:function(){
        if (this.isHit) {
            return;
        }
        this.isHit = true;
        this.roleState = State.hurt;
       // this.lv = this.rlv.linearVelocity;

        if (this.node.scaleX == 1) {
            this.lv.x = -100;
        }else if (this.node.scaleX == -1) {
            this.lv.x = 100
        }
        this.rlv.linearVelocity = this.lv;
        this.setAnim('hurt');

    },
    // isDie(){
      
    // },

     update (dt) {
        this.now_Time += dt;
        if (this.now_Time >= this.MprestoreTime) {
            this.mp ++;
            this.now_Time =0;
            if (this.mp >= 5) {
                this.mp = 5;
            }
            this.mpbar.progress += 1/this.maxMp;
            if (this.mpbar.progress == 1) {
                this.mpbar.progress = 1;
            }
            this.showMp(this.mp+"/"+this.maxMp);
        }

        switch (this.roleState) {
            case State.stand:
                if (Input[cc.macro.KEY.j] || Input[cc.macro.KEY.k] || Input[cc.macro.KEY.u] ) {
                    this.roleState= State.attack;
                }else if (Input[cc.macro.KEY.w]) {
                    this.roleState = State.jump;
                // }else if ((Input[cc.macro.KEY.d] || Input[cc.macro.KEY.a]) && Input[cc.macro.KEY.s] && Input[cc.macro.KEY.k]) {
                //     this.roleState = State.attackSlide;
                }
                break;
        }

        

        if (this.roleState == State.attack) {
            //cc.director.getCollisionManager().enabled = true;
            this.setAttack();
            if (this.attactCD > 0 ) {
                this.attactCD --;
            }
            if (this.daggerCD >0) {
                this.daggerCD --;
            }
        }else if (this.roleState == State.stand) {
            this.move();
            this.run = true;
            
        }else if (this.roleState == State.jump) {
            this.setJump();
            if (this.jumpCD >0) {
                this.jumpCD --;
            }
            if (this.attAirCD > 0) {
                this.attAirCD --;
            }
        }else if (this.roleState == State.attackSlide) {
            this.setAttackSlide();
        }
        
     },
});
